
from fastapi import FastAPI,APIRouter,Request,UploadFile,File
from json.decoder import JSONDecodeError
import sys 
sys.path.append('..')
import uuid
from utils.http_response import HttpResponse 
from utils.db import engine
from minio import Minio
ak = "jBEqfAG6lAUMYqhf9ZuQ"
sk = 'zzh0117.'

client = Minio(
    "113.31.110.212:9000",
    secure=False,
    access_key=ak,
    secret_key=sk,
)

router = APIRouter(prefix="/other",tags=["other"])


@router.post("/upload")
def upload_file(file: UploadFile):
    # file.filename = str(uuid.uuid4()) + file.filename
    bucket_name = 'health'
    with open(f'./upload/'+file.filename, "wb") as buffer:
        buffer.write(file.file.read())
    try:
        if not client.bucket_exists(bucket_name):
            client.make_bucket(bucket_name)
        client.fput_object(bucket_name,file.filename,'./upload/'+file.filename)
    except Exception  as e :
        print(e)
    return {"code":200,"message":"success","data":"http://113.31.110.212:9000/health/"+file.filename}